<html>
<head>
<title>Main.java</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style type="text/css">
.s0 { color: #cf8e6d;}
.s1 { color: #bcbec4;}
.s2 { color: #bcbec4;}
.s3 { color: #6aab73;}
.s4 { color: #7a7e85;}
.s5 { color: #2aacb8;}
</style>
</head>
<body bgcolor="#1e1f22">
<table CELLSPACING=0 CELLPADDING=5 COLS=1 WIDTH="100%" BGCOLOR="#606060" >
<tr><td><center>
<font face="Arial, Helvetica" color="#000000">
Main.java</font>
</center></td></tr></table>
<pre><span class="s0">import </span><span class="s1">java</span><span class="s2">.</span><span class="s1">io</span><span class="s2">.</span><span class="s1">BufferedReader</span><span class="s2">;</span>
<span class="s0">import </span><span class="s1">java</span><span class="s2">.</span><span class="s1">io</span><span class="s2">.</span><span class="s1">FileReader</span><span class="s2">;</span>
<span class="s0">import </span><span class="s1">java</span><span class="s2">.</span><span class="s1">io</span><span class="s2">.</span><span class="s1">IOException</span><span class="s2">;</span>
<span class="s0">import </span><span class="s1">java</span><span class="s2">.</span><span class="s1">util</span><span class="s2">.</span><span class="s1">ArrayList</span><span class="s2">;</span>
<span class="s0">import </span><span class="s1">java</span><span class="s2">.</span><span class="s1">util</span><span class="s2">.</span><span class="s1">List</span><span class="s2">;</span>

<span class="s0">public class </span><span class="s1">AveragePriceCalculator </span><span class="s2">{</span>
    <span class="s0">public static void </span><span class="s1">main</span><span class="s2">(</span><span class="s1">String</span><span class="s2">[] </span><span class="s1">args</span><span class="s2">) {</span>
        <span class="s1">String filePath </span><span class="s2">= </span><span class="s3">&quot;prices.txt&quot;</span><span class="s2">; </span><span class="s4">// 文件路径</span>
        <span class="s1">List</span><span class="s2">&lt;</span><span class="s1">Integer</span><span class="s2">&gt; </span><span class="s1">prices </span><span class="s2">= </span><span class="s0">new </span><span class="s1">ArrayList</span><span class="s2">&lt;&gt;(); </span><span class="s4">// 存储价格</span>

        <span class="s0">try </span><span class="s2">(</span><span class="s1">BufferedReader reader </span><span class="s2">= </span><span class="s0">new </span><span class="s1">BufferedReader</span><span class="s2">(</span><span class="s0">new </span><span class="s1">FileReader</span><span class="s2">(</span><span class="s1">filePath</span><span class="s2">))) {</span>
            <span class="s1">String line</span><span class="s2">;</span>
            <span class="s0">while </span><span class="s2">((</span><span class="s1">line </span><span class="s2">= </span><span class="s1">reader</span><span class="s2">.</span><span class="s1">readLine</span><span class="s2">()) != </span><span class="s0">null</span><span class="s2">) {</span>
                <span class="s4">// 获取价格字符串并去除末尾的&quot;元/台&quot;</span>
                <span class="s1">String priceStr </span><span class="s2">= </span><span class="s1">line</span><span class="s2">.</span><span class="s1">replaceAll</span><span class="s2">(</span><span class="s3">&quot;[^0-9]&quot;</span><span class="s2">, </span><span class="s3">&quot;&quot;</span><span class="s2">);</span>
                <span class="s4">// 将价格字符串转换为整数并添加到列表中</span>
                <span class="s0">if </span><span class="s2">(!</span><span class="s1">priceStr</span><span class="s2">.</span><span class="s1">isEmpty</span><span class="s2">()) {</span>
                    <span class="s1">prices</span><span class="s2">.</span><span class="s1">add</span><span class="s2">(</span><span class="s1">Integer</span><span class="s2">.</span><span class="s1">parseInt</span><span class="s2">(</span><span class="s1">priceStr</span><span class="s2">));</span>
                <span class="s2">}</span>
            <span class="s2">}</span>
        <span class="s2">} </span><span class="s0">catch </span><span class="s2">(</span><span class="s1">IOException e</span><span class="s2">) {</span>
            <span class="s1">e</span><span class="s2">.</span><span class="s1">printStackTrace</span><span class="s2">();</span>
        <span class="s2">}</span>

        <span class="s4">// 计算平均价格</span>
        <span class="s0">double </span><span class="s1">averagePrice </span><span class="s2">= </span><span class="s1">prices</span><span class="s2">.</span><span class="s1">stream</span><span class="s2">().</span><span class="s1">mapToInt</span><span class="s2">(</span><span class="s1">Integer::intValue</span><span class="s2">).</span><span class="s1">average</span><span class="s2">().</span><span class="s1">orElse</span><span class="s2">(</span><span class="s5">0</span><span class="s2">);</span>
        <span class="s1">System</span><span class="s2">.</span><span class="s1">out</span><span class="s2">.</span><span class="s1">println</span><span class="s2">(</span><span class="s3">&quot;Average price is: &quot; </span><span class="s2">+ </span><span class="s1">averagePrice </span><span class="s2">+ </span><span class="s3">&quot;元&quot;</span><span class="s2">);</span>
    <span class="s2">}</span>
<span class="s2">}</span></pre>
</body>
</html>